package com.gton.controller;

import com.gton.commons.ResultAdvice;
import com.gton.commons.myenum.ExcptionEnum;
import com.gton.entity.LoginUser;
import com.gton.service.UserService;
import com.gton.uitls.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @program: springcloud-nacos
 * @description: 用户请求处理的controller
 * @author: GuoTong
 * @create: 2020-11-20 16:23
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultAdvice login(@RequestBody LoginUser admin){
        System.out.println("用户进入校验。。。。");
        boolean result = userService.isNameAndPasswordOk(admin);//验证密码是否正确
        if (result){
            //密码是正确的
            //生成jwt令牌,返回到客户端
            Map<String,String> info = new HashMap<>();
            info.put("username",admin.getUsername());
            //基于工具类生成jwt令牌
            String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), admin.getUsername(), null);
            info.put("token",jwt);
           // return new Result(true, StatusCodeEnum.OK,info);
            return ResultAdvice.OK(info);
        }else{
            //密码错误
            return ResultAdvice.error(ExcptionEnum.PWD_OR_USERNAME_ERROR);
        }
    }
}
